package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.qo.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class EsSearchController {

    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private ISearchService searchService;
    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo")SearchQueryObject qo){
       switch (qo.getType()){
           case SearchQueryObject.SEARCH_TYPE_DEST:
               return searchDest(model,qo);
           case SearchQueryObject.SEARCH_TYPE_STRATEGY:
               return searchStrategy(model,qo);
           case SearchQueryObject.SEARCH_TYPE_TRAVEL:
               return searchTravel(model,qo);
           case SearchQueryObject.SEARCH_TYPE_USER:
               return searchUser(model,qo);
           default:
               return searchAll(model,qo);
       }
    }

    /**
     * 地点精确查询
     * @param model
     * @param qo
     * @return
     */
    private String searchDest(Model model, SearchQueryObject qo) {
        DestinationTemplate dest = destinationSearchService.findByName(qo.getKeyword());
        if (dest!=null){
            SearchResultVO data = new SearchResultVO();
            List<StrategyTemplate> sts = strategySearchService.findByDestName(qo.getKeyword());
            data.setTotal(data.getTotal() + sts.size());
            if (sts.size()>5){
                sts = sts.subList(0,5);
            }
            List<TravelTemplate> ts = travelSearchService.findByDestName(qo.getKeyword());
            data.setTotal(data.getTotal() + ts.size());
            if (ts.size()>5){
                ts = ts.subList(0,5);
            }
            List<UserInfoTemplate> us = userInfoSearchService.findByDestName(qo.getKeyword());
            data.setTotal(data.getTotal() + us.size());
            if (us.size()>5){
                us = us.subList(0,5);
            }
            data.setStrategys(sts);
            data.setTravels(ts);
            data.setUsers(us);
            model.addAttribute("data",data);
        }
        model.addAttribute("dest",dest);
        return "/index/searchDest";
    }

    /**
     * 以下都是关键字查询
     * @param model
     * @param qo
     * @return
     */
    private String searchStrategy(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page",page);
        return "/index/searchStrategy";
    }
    private String searchTravel(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page",page);
        return "/index/searchTravel";
    }
    private String searchUser(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page",page);
        return "/index/searchUser";
    }
    private String searchAll(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> sts = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");

        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");

        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "nickname", "destName");

        SearchResultVO data = new SearchResultVO();
        data.setStrategys(sts.getContent());
        data.setTravels(ts.getContent());
        data.setUsers(us.getContent());
        data.setDests(dests.getContent());

        model.addAttribute("data",data);
        return "/index/searchAll";
    }
}
